package com.myjob.response;

import lombok.*;

import java.io.Serializable;

/**
 * 作为返回给前端的响应体
 * @author Chen Yuan
 * @create 2021-01-08  15:51
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {
    // 处理的数据
    private T datas;
    // 返回的结果编码
    private Integer resp_code;
    // 返回的结果信息
    private String resp_msg;

    // 执行成功，返回的信息
    public static <T> Result<T> succeed(String msg) {
        return succeedWith(null, CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> Result<T> succeed(T model, String msg) {
        return succeedWith(model, CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> Result<T> succeed(T model) {
        return succeedWith(model, CodeEnum.SUCCESS.getCode(), "");
    }

    public static <T> Result<T> succeedWith(T datas, Integer code, String msg) {
        return new Result<>(datas, code, msg);
    }

    // 执行失败，返回的信息
    public static <T> Result<T> failed(String msg) {
        return failedWith(null, CodeEnum.FAIL.getCode(), msg);
    }

    public static <T> Result<T> failed(T model, String msg) {
        return failedWith(model, CodeEnum.FAIL.getCode(), msg);
    }

    public static <T> Result<T> failedWith(T datas, Integer code, String msg) {
        return new Result<>(datas, code, msg);
    }

    // 执行异常，返回的消息
    public static <T> Result<T> error(String msg) {
        return errorWith(null, CodeEnum.ERROR.getCode(), msg);
    }

    public static <T> Result<T> error(T model, String msg) {
        return errorWith(model, CodeEnum.ERROR.getCode(), msg);
    }

    public static <T> Result<T> errorWith(T datas, Integer code, String msg) {
        return new Result<>(datas, code, msg);
    }

    // 警告信息
    public static <T> Result<T> warn(String msg) {
        return warnWith(null, CodeEnum.WARN.getCode(), msg);
    }

    public static <T> Result<T> warn(T model, String msg) {
        return warnWith(model, CodeEnum.WARN.getCode(), msg);
    }

    public static <T> Result<T> warnWith(T datas, Integer code, String msg) {
        return new Result<>(datas, code, msg);
    }

    // 执行代码的枚举类型
    public enum CodeEnum {
        SUCCESS(0),
        ERROR(-9),
        FAIL(1),
        WARN(-1);

        private Integer code;

        CodeEnum(Integer code) {
            this.code = code;
        }

        public Integer getCode() {
            return code;
        }
    }

    // 判断执行结果

    public Boolean isSuccess() {
        return CodeEnum.SUCCESS.getCode().equals(this.resp_code);
    }

    public Boolean isFailed() {
        return CodeEnum.FAIL.getCode().equals(this.resp_code);
    }

    public Boolean isError() {
        return CodeEnum.ERROR.getCode().equals(this.resp_code);
    }

    public Boolean isWarn() {
        return CodeEnum.WARN.getCode().equals(this.resp_code);
    }


}
